package com.code.zjm.demo.adapt;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;
import java.util.Locale;

/**
 * Created by ZJM on 2017/3/3.
 */

public class CustomBaseAdapter extends BaseAdapter {

    private Context context;
    private List<CustomListCellData> cellData;

    public CustomBaseAdapter(Context context, List<CustomListCellData> cellData) {
        this.context = context;
        this.cellData = cellData;
    }

    @Override
    public int getCount() {
        return cellData.size();
    }

    @Override
    public Object getItem(int position) {
        return cellData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CustomListCellData data = cellData.get(position);
        LinearLayout cellLayout;
        if (convertView != null) {  //获取系统回收的对象
            cellLayout = (LinearLayout) convertView;
        } else {
            cellLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.subview_of_base, null);
        }
        ImageView userImage = (ImageView) cellLayout.findViewById(R.id.userImage);
        TextView userName = (TextView) cellLayout.findViewById(R.id.userName);
        TextView ageAndGender = (TextView) cellLayout.findViewById(R.id.ageAndSex);

        userImage.setImageResource(data.getImage());
        userName.setText(String.format(Locale.ENGLISH, "User Name:%s", data.getUserName()));
        ageAndGender.setText(String.format(Locale.ENGLISH, "Age:%d || Gender:%s", data.getAge(),
                data.getGender()));

        return cellLayout;
    }
}
